#import "XTDemo.h"

@implementation XTDemo
- init
{
	[super init];
	demoAction = [[XTDispatchAction alloc] init];
	fieldAction = [[XTDispatchAction alloc] init];
	[fieldAction addBindings:"cs'Q=insertKeyCombOfNextKey" estream:nil];
	return self;
}

- appDidInit:sender
{
	id my_font = [Font newFont:"Ohlfs" size:14.0];
	
	demoText = [demoText docView];
	[demoText setFont:my_font];
	[demoText setInitialAction:demoAction];
	[demoText setSel:0 :0];
	[[demoText window] makeKeyAndOrderFront:self];
	return self;
}

- windowWillReturnFieldEditor:sender toObject:client
{	
	id field_font = [Font newFont:"Ohlfs" size:12.0];

	fieldText = [XText newFieldEditorFor:sender
					initialAction:fieldAction
					estream:nil];

	[fieldText setFont:field_font];

	return fieldText;
}	

- applyBinding:sender
{
	[demoAction addBindings: [cmdField stringValue]
		estream:[demoText errorStream]];
    return self;
}

- insertKey:sender
{
	[fieldText setSel:0 :0];
	[fieldText insertKeyCombOfNextKey];	

	return self;
}

- clearBindings:sender
{
	id newAction;
				
	/* if anyone thinks this will cause a memory leak, let me know P.G. */
	newAction = [[XTDispatchAction alloc] init];   
	[demoText setInitialAction:newAction];				
	[demoAction free];
	demoAction = newAction;
	
	return self;

}

- openFile:sender
{
  const char *file;
  char path[MAXPATHLEN +1];
  id openPanel;  

  [[NXBundle mainBundle] getPath:path forResource:"KeyBindingFiles" ofType:""];

  openPanel = 
  	[[[OpenPanel new] allowMultipleFiles:NO] setTitle:"Keybinding File"];

	/* run the open panel */
	if ([openPanel runModalForDirectory:path file:""]){

    	/* open the file returned by the save panel */
    	file = [openPanel filename];
    	strcpy(fullName, file);
		[loadButton setEnabled:YES];	
		sprintf(titleString, "Keybinding File: %s", *[openPanel filenames]);
   		[[demoText window] setTitle:titleString];

		/* open file from the workspace-->default editor) */
		[[Application workspace] openFile:file];
	}
	
	return self;
}
  
- saveBinding:sender
{
	FILE *fp;
	const char *file;
	id savePanel;  
	
	if(![loadButton isEnabled]) {
		savePanel = [[SavePanel new] setTitle:"Keybinding File"];
		if ([savePanel runModal]) {

    		/* use the file returned by the save panel */
    		file = [savePanel filename];
    		strcpy(fullName, file);
			sprintf(titleString, "Keybinding File: %s", [savePanel filename]);
   			[[demoText window] setTitle:titleString];
			[loadButton setEnabled:YES];
		}
		else return self;
	}
	
	fp = fopen(fullName, "a+"); /* append */
	
	if(!fp){
		NXRunAlertPanel("Save Binding","Can't Append to %s", "Okay", 
		NULL, NULL, fullName);
		return self;
	}
	
	/* write the comment (rather primitively)*/
	fprintf(fp,"%s","#");
	fprintf(fp,"%s",[comField stringValue]);
	fprintf(fp,"\n");

	/* write the binding */
	fprintf(fp,"%s",[cmdField stringValue]);
	fprintf(fp,"\n");
	
	fclose(fp);
	
	return self;
}	

- loadBindings:sender
{		
	[demoAction loadFromFile:fullName estream:nil];		
	return self;
}
@end
